<?php

namespace app\common\logic;


use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\model\Config;
use app\common\model\Member;
use app\common\model\Order;
use app\common\model\ServiceTimes;
use http\Env;
use think\Exception;

class MemberLogic extends BaseLogic
{
    public $model;

    public function assignModel()
    {
        $this->model          = new Member();
    }

    /**
     * 车主端 我的信息
     * @return mixed
     */
    public function info()
    {
        $member_info = $this->model::get(MemberVo::instance()->getMemberId());
        return ['member_name'=>$member_info['member_name'],'birthday'=> empty($member_info['birthday']) ? '' : $member_info['birthday']*1000,'member_sex'=>$member_info['member_sex'],'member_mobile'=>!empty($member_info['member_mobile']) ? substr_replace($member_info['member_mobile'],'****',3,4) : ''];
    }

    /**
     * 更新用户名称
     * @param $params
     * @return bool
     * @throws ApiException
     */
    public function member_name($params)
    {
        try{
            $this->model->update(['member_name'=>$params['member_name']],['id'=>MemberVo::instance()->getMemberId()]);
            return true;
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }
    }

    /**
     * 设置生日
     * @param $params
     * @return bool
     * @throws ApiException
     */
    public function birthday($params)
    {
        try{
            $member_info = $this->model::get(MemberVo::instance()->getMemberId());
            if (!empty($member_info['birthday'])){
                throw new Exception('生日已设置，不可修改');
            }
            $this->model->update(['birthday'=>$params['date']],['id'=>MemberVo::instance()->getMemberId()]);
            return true;
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }
    }

    /**
     * 设置性别
     * @param $params
     * @return bool
     * @throws ApiException
     */
    public function sex($params)
    {
        try{
            $this->model->update(['member_sex'=>$params['sex']],['id'=>MemberVo::instance()->getMemberId()]);
            return [];
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }
    }

    /**
     * 设置头像
     * @param $params
     * @return bool
     * @throws ApiException
     */
    public function head($params)
    {
        try{
            $this->model->update(['member_avatar'=>$params['member_avatar']],['id'=>MemberVo::instance()->getMemberId()]);
            return [];
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }
    }

    /**
     * 余额
     * @return mixed
     * @throws ApiException
     */
    public function balance()
    {
        try{
            $info = $this->model::get(MemberVo::instance()->getMemberId());
            return $info['balance'];
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }
    }

    /**
     * 我的权益
     * @return array
     * @throws ApiException
     */
    public function permission()
    {
        $data = [];
        try{
            $member_info = $this->model::get(MemberVo::instance()->getMemberId());
            if (empty($member_info))
                throw new Exception('用户信息不存在');
            $data['is_vip'] = $member_info['is_vip'];//是否是vip 0不是 1是
            $vip_discount = Config::where(['name'=>'vip_discount'])->value('value');//vip折扣
            $data['vip_discount'] = $vip_discount *10;
            $service_times_info = ServiceTimes::get(['member_id'=>$member_info['id']]);
            $service_num = Config::where(['name'=>'service_num'])->value('value');//系统设置的达标次数
            $days = intval(\think\Env::get('app.days',180));
            $days = $days-1;
            $data['service_times'] = Order::whereBetween('audit_time',[strtotime(date('Y-m-d',strtotime("-{$days} days"))),time()])->where(['member_id'=>MemberVo::instance()->getMemberId()])->whereIn('order_status',[3,4])->count('id');//当前时间往前推180的总完成数
            if ($member_info['is_vip'] == 1){
                $data['vip_end_time'] = date('Y-m-d',$service_times_info['end_time']);//vip 过期时间
                $data['surplus_times'] = intval(bcsub($service_num,$data['service_times']));//剩余次数
            }else{
                $data['vip_end_time'] = '';//vip 过期时间
                $data['surplus_times'] = intval(bcsub($service_num,$data['service_times'],0));//剩余次数
            }
            if ($data['surplus_times'] < 0)
                $data['surplus_times'] =0;
            $data['all_times'] = intval($service_num);
            $data['vip_rule'] = Config::where('name','vip_rule')->value('value');
            $data['permission'] = [
                ['image'=>\think\Env::get('app.app_host').'/uploads/permission/123.png','desc'=>"洗车服务达到{$service_num}次，升级到尊贵的VIP会员，即可获得专享权益"]
            ];
            return $data;
        }catch (Exception $e){
            throw new ApiException($e->getMessage(),404);
        }
    }




}